<?php
declare (strict_types=1);

namespace app\common\model;

use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;

class ContractBonusPoolRecord extends Base
{
    protected $name = 'contract_bonus_pool_record';

    public static $_type = [
        1 => '银牌分红池',
        2 => '金牌分红池',
        3 => '钻石、金钻、黑金钻分红池',
    ];

    public function getTypeTextAttr($value, $data): string
    {
        return isset(self::$_type[$data['type']]) ? self::$_type[$data['type']] : '-';
    }

    public function orders()
    {
        return $this->belongsTo(Order::class, 'order_id', 'id')->field('id,sn');
    }

    /**
     * 分红池入池记录：用户确认收货7天后入池
     * @param $order
     * @return array
     */
    public static function addBonusPoolRecord($order): array
    {
        $rate['silver'] = Cache::get('system')['contract_share_silver_rate'];
        $rate['gold'] = Cache::get('system')['contract_share_gold_rate'];
        $rate['diamond'] = Cache::get('system')['contract_share_diamond_rate'];
        try {
            Db::transaction(function () use ($order, $rate) {
                $model = new ContractBonusPoolRecord();
                $silver_amount = roundNum($order->profit * $rate['silver'], 2);
                $gold_amount = roundNum($order->profit * $rate['gold'], 2);
                $diamond_amount = roundNum($order->profit * $rate['diamond'], 2);
                $datas = [
                    [
                        'pool_id' => 1,
                        'type' => 1,
                        'profit_rate' => $rate['silver'],
                        'amount' => $silver_amount,
                    ],[
                        'pool_id' => 2,
                        'type' => 2,
                        'profit_rate' => $rate['gold'],
                        'amount' => $gold_amount,
                    ],[
                        'pool_id' => 3,
                        'type' => 3,
                        'profit_rate' => $rate['diamond'],
                        'amount' => $diamond_amount,
                    ]
                ];

                $add = [
                    'order_id' => $order->id,
                    'total_profit' => $order->profit,
                ];
                array_walk($datas, function (&$value, $key, $add) {
                    $value = array_merge($value, $add);
                }, $add);

                $model->insertAll($datas);

                ContractBonusPool::where('type',1)->inc('total_amount', floatval($silver_amount))->update();
                ContractBonusPool::where('type',2)->inc('total_amount', floatval($gold_amount))->update();
                ContractBonusPool::where('type',3)->inc('total_amount', floatval($diamond_amount))->update();

                $order->is_pooled = 1;
                $order->save();
            });

        }catch (\Exception $e){
            Log::write("addBonusPoolRecord>>".$e->getMessage());
            return ['code' => -1, 'msg' => $e->getMessage()];
        }

        return ['code' => 1, 'msg' => '合约专区分红池入池执行成功'];
    }
}
